
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ObjectPoolManager : MonoBehaviour
{

    // --- 大坑! 容易加载场景后直接整个卡死, 还无法找到错误原因...
    // static string obj_name = "ObjectPoolManager";  // 为instance自动初始化一个obj
    // static string parent_path = "/scripts";  // 统一父级节点

    // [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    // static void init_instance()
    // {
    //     GameObject parent = BdTools.get_game_object_by_path(parent_path);
    //     if (parent == null)
    //         parent = new GameObject(parent_path);

    //     GameObject obj_i = BdTools.get_game_object_by_path(obj_name, parent: parent);
    //     if (obj_i == null)
    //     {
    //         obj_i = new GameObject(obj_name);
    //         obj_i.transform.parent = parent.transform;
    //     }

    //     var comp_i = obj_i.GetComponent<ObjectPoolManager>();
    //     if (comp_i == null)
    //         obj_i.AddComponent<ObjectPoolManager>();

    //     obj_i.SetActive(true);
    // }

    // static ObjectPoolManager _instance;
    // public static ObjectPoolManager instance
    // {
    //     get
    //     {
    //         return _instance;
    //     }
    //     set
    //     {
    //         _instance = value;
    //     }
    // }

    public static ObjectPoolManager instance;

    void Awake()
    {
        // BdUtils.log(" --- ObjectPoolManager.Awake");
        if (instance != null)
            Destroy(this);
        instance = this;
    }
    // public ParticleSystem testParticleEffect;
    // ParticleSystem effect;
    GameObject effect_obj;

    public bool debug = false;

    // public GameObject prefab_obj;
    void Start()
    {

    }
    void Update()
    {
        // effect = Instantiate(testParticleEffect, transform.position + Vector3.up* 0.5f, Quaternion.identity);

        if (debug && Input.GetKeyDown(KeyCode.V))
        {
            // parameter.getHit = true;
            // TransitionState(StateType.Death);
            BdUtils.log("TestBloodEffectPool -- V");


            //从对象池中获取对象
            // GameObject effect_obj = ObjectPool.instance.GetObj("heart", "inventory/prefabs/heart");
            GameObject effect_obj = ObjectPool.instance.GetObj("BloodEmissionEffect");
            // GameObject effect_obj = ObjectPool.instance.GetObj("BloodEmissionEffect", prefab_obj);
            // var result = Instantiate(prefab_obj);

            // var effect_comp = effect_obj.GetComponent<ParticleSystem>();
            // effect_comp.Simulate(0.0f);    //把粒子特效暂停在某一固定时间，0.0f就是刚开始的时候
            // effect_comp.Play();                 //再播放，相当于重新开始播放

            // bullet.transform.position = Camera.main.transform.position;
            // bullet.GetComponent<Rigidbody>().velocity = dir.normalized * speed;

            StartCoroutine(AutoRecycle(effect_obj, 1));
        }
        // Transform effect_trans = transform.Find("EmissionEffect");
        // GameObject effect_obj = effect_trans.gameObject;

        // effect_obj.SetActive(true);
    }

    public void recycle_obj(GameObject obj_i, float time = 3f)
    {
        // BdUtils.log(" recycle_obj --- obj_i, time:", obj_i, time);
        StartCoroutine(AutoRecycle(obj_i, time));
    }

    public void destroy_obj(GameObject obj_i, float time = 3f)
    {
        if (obj_i == null)
            return;
        // BdUtils.log(" destroy_obj --- obj_i, time:", obj_i, time);
        StartCoroutine(DestroyObject(obj_i, time));
    }

    /// <summary>
    /// 3秒后自动回收到对象池
    /// </summary>
    /// <returns></returns>
    IEnumerator AutoRecycle(GameObject obj_i, float time = 3f)
    {
        yield return new WaitForSeconds(time);

        ObjectPool.instance.RecycleObj(obj_i);
    }

    IEnumerator DestroyObject(GameObject obj_i, float time = 3f)
    {
        yield return new WaitForSeconds(time);

        Destroy(obj_i);
    }
}
